package com.socket.server.utils;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @ProjectName: netty_socket
 * @Package: com.socket.server.utils
 * @ClassName: ${CLASS_NAME}
 * @Author: xiahuihui
 * @Description: ${description}
 * @Date: 2020/6/11 0011 15:07
 * @Version: 1.0
 */

public class MessageUtil implements Runnable {
    private Logger log = LogManager.getLogger(getClass());

    private String message;


    private Object lock;


    public MessageUtil(Object lock, String message) {
        this.lock = lock;
        this.message = message;
    }


    public void dealMessage(String message) {
        NettyServerHandler nettyServerHandler = new NettyServerHandler();
        String[] msgcontent = message.split("@");
        String regex = "1[0-9]{14}";
        if (!msgcontent[0].matches(regex) || msgcontent.length < 2) {
            log.info("this message is invalid");
            return;
        }
//        if (!clientTalbles.containsKey(msgcontent[0])) {
//            clientTalbles.put(msgcontent[0],new ClientTask());
//        }
//        this.clientTask = clientTalbles.get(msgcontent[0]);
//        if (clientTask.getDeviceId() == null) {
//            this.clientTask.setDeviceId(msgcontent[0]);
//        }
//        this.clientTask.setIpAddreass(this.client.getInetAddress().getHostAddress());

        switch (msgcontent[1]) {  //TODO: wanghui 接收到客户端发送的消息进行处理
            case "scanfreqs":

                NettyServerHandler.reportFreqs.add(msgcontent[0] + "@" + msgcontent[2]);
                log.info(NettyServerHandler.reportFreqs.toString() + "*************************");
                synchronized (lock) {
                    lock.notify();
                    log.info("已经通知周期任务线程！！！，发送调频任务！！！");
                }
                break;
            case "tune":
                String time = "30";
                String tuneMessage = "recording#" + msgcontent[2] + "#" + time;
                try {
                    nettyServerHandler.channelWrite(NettyServerHandler.deviceMap.get(msgcontent[0]), tuneMessage);
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("周期任务发送录音指令操作异常！！！");
                }
                break;
            case "recording":
                synchronized (lock) {
                    log.info("我要唤醒小哥！！！");
                    lock.notify();
                }
                break;
            case "startreport":
                break;
            case "devicereport":
                break;
            case "connect":
                log.info("设备重新连接，继续执行任务！！！");
                lock.notify();
                break;
            default:
                break;
        }
    }


    @Override
    public void run() {
        synchronized (lock) {
            NettyServerHandler nettyServerHandler = new NettyServerHandler();
            String[] msgcontent = message.split("@");
            String regex = "1[0-9]{14}";
            if (!msgcontent[0].matches(regex) || msgcontent.length < 2) {
                log.info("this message is invalid");
                return;
            }
//        if (!clientTalbles.containsKey(msgcontent[0])) {
//            clientTalbles.put(msgcontent[0],new ClientTask());
//        }
//        this.clientTask = clientTalbles.get(msgcontent[0]);
//        if (clientTask.getDeviceId() == null) {
//            this.clientTask.setDeviceId(msgcontent[0]);
//        }
//        this.clientTask.setIpAddreass(this.client.getInetAddress().getHostAddress());

            switch (msgcontent[1]) {  //TODO: wanghui 接收到客户端发送的消息进行处理
                case "scanfreqs":
//                freqs = msgcontent[2].split("#");
//                for (String s : freqs) {
//                    if (s.length() < 3) {
//                        continue;
//                    }
////                    NettyServerHandler.getReportFreqs().offer(s);
//                    reportFreqs.offer(s);}
                    log.info("已经通知周期任务线程！！！，发送调频任务！！！");


                    break;
                case "tune":
                    String time = "30";
                    String tuneMessage = "recording#" + msgcontent[2] + "#" + time;
                    try {
//                    synchronized (dutyLock) {
                        log.info("向设备" + msgcontent[0] + "：" + NettyServerHandler.deviceMap.get(msgcontent[0]) + "发送录音任务！！！！");
                        nettyServerHandler.channelWrite(NettyServerHandler.deviceMap.get(msgcontent[0]), tuneMessage);
//                    Thread.sleep(Integer.parseInt(time) * 1000);
                        //唤醒周期任务中的调频中的wait线程
//                    PeriodicDuty periodicDuty = new PeriodicDuty();
//                    periodicDuty.runThread();
//                    dutyLock.notify();
//                    log.info("我已经将锁释放！！！");
//                    }
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error("周期任务发送录音指令操作异常！！！");
                    }
                    break;
                case "recording":
                    lock.notify();
                    log.info("小哥醒醒，别睡了了，要干活了！！！");
                    break;
                case "startreport":
                    break;
                case "devicereport":
                    break;
                case "connect":
                    log.info("设备重新连接，继续执行任务！！！");
                    lock.notify();
                    break;
                default:
                    break;
            }
        }
    }


}
